package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mapper.PermissionMapper;
import com.pageResult.PageResult;
import com.pojo.Permission;
import com.pojo.Role;
import com.query.QueryPageBean;
import com.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public List<Permission> queryAll() {
        return permissionMapper.selectByExample(null);
    }

    @Override
    public PageResult selectByQueryString(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<Permission> page = permissionMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.insertSelective(permission);
    }

    @Override
    public Permission queryById(Integer id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public void delete(Integer id) throws CustomException {
        Integer count = permissionMapper.queryByCount(id);
        if(count>0){
            throw new CustomException(MessageConstant.ROLE_PERMISSION_USE);
        }
        permissionMapper.deleteByPrimaryKey(id);
    }
}
